package com.yaoayao.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yaoayao.mall.common.result.Result;
import com.yaoayao.mall.model.product.*;
import com.yaoayao.mall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: peiyao
 * @date: 2024/4/12 18:57
 * @description:
 */
@RestController
//@CrossOrigin
@RequestMapping("admin/product")
public class ManageController {

    @Autowired
    private ManageService manageService;

    //获取一级分类
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List =  manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    //获取二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> baseCategory2List =  manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    //获取三级分类  select * from 表 where id = '1'  类型转换  不使用索引
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> baseCategory3List =  manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //根据分类查询平台属性及属性值
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id
    ){
        List<BaseAttrInfo>  baseAttrInfoList =  manageService.attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    //保存平台属性
    // rest风格开发 原则 ： Get 查询 POst添加  PUT 修改 Delete删除
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();//全局异常处理器
    }

    //品牌列表查询 分页
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Integer page,
            @PathVariable Integer limit
    ){
        IPage iPage = manageService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }

    //获取商品集合分页查询
    @GetMapping("/{page}/{limit}")
    public Result findSpuList(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            Long category3Id
    ){
        IPage iPage = manageService.findSpuList(page,limit,category3Id);
        return Result.ok(iPage);
    }

    //查询所有品牌列表
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList =  manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //查询销售属性列表
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList =  manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //保存SPU
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //查询SPU所有图片 显示在SKU添加页面上 供其选择
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList =  manageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //根据SPUID查询所有销售属性及属性值表
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //保存SKU
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);

        return Result.ok();
    }

    //查询SKU分页
    @GetMapping("/list/{page}/{limit}")
    public Result skuList(  @PathVariable Integer page,
                            @PathVariable Integer limit){
        IPage iPage = manageService.skuList(page,limit);
        return Result.ok(iPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }

}
